package com.example.client.controller.comment;

import com.example.commentservice.service.CommentService;
import com.example.commonapi.pojo.Comment;
import com.example.commonapi.pojo.UAC;
import com.example.commonapi.pojo.User;
import com.example.userservice.service.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class CommentController {
    private static final Log logger = LogFactory.getLog(CommentController.class);

    @Reference(version = "${demo.service.version}")
    private CommentService commentService;

    @Reference(version = "${demo.service.version}")
    private UserService userService;

    @RequestMapping("/comment/history/{userId}")
    public String commentHistory(@PathVariable("userId") String userId, HttpServletRequest request) {
        Integer id = Integer.valueOf(userId);
        List<UAC> uacs = commentService.getUACByUserId(id);
        User user = userService.getUserById(id);

        request.setAttribute("uacs", uacs);
        request.setAttribute("user", user);

        return "user/user-comment-manage";
    }

    @RequestMapping("/comment/edit/{commentId}")
    public String commentEdit(@PathVariable("commentId") String commentId, HttpServletRequest request) {
        Comment comment = commentService.getCommentById(Integer.valueOf(commentId));
        User user = userService.getUserByCommentId(Integer.valueOf(commentId));

        request.setAttribute("comment", comment);
        request.setAttribute("user", user);

        return "user/user-comment-edit";
    }
}
